#include "timeout.h"

int timeoutInit(timeout_t* out){
    for(int i = 0;i < 30;i++){
        out->setNum[i] = 0;
        out->setArr[i] = (int*)malloc(sizeof(int)*MAX_SET_SIZE);
        memset(out->setArr[i],-1,sizeof(int)*MAX_SET_SIZE);
    }
    memset(out->fdToArr,-1,sizeof(int)*1024);
    return 0;
}

int popSet(timeout_t* out,int netfd,int set_pointer){
    for(int i = 0;i < MAX_SET_SIZE;i++){
        if(out->setArr[set_pointer][i] != netfd){
            continue;
        }
        else{
            out->setArr[set_pointer][i] = -1;
            break;
        }
    }
    return 0;
}

int pushSet(timeout_t* out,int netfd,int set_pointer){
    if(out->fdToArr[netfd] == -1){
        out->fdToArr[netfd] = set_pointer;
        out->setArr[set_pointer][out->setNum[set_pointer]] = netfd;
        out->setNum[set_pointer]++;
        return 0;
    }
    popSet(out,netfd,out->fdToArr[netfd]);
    out->fdToArr[netfd] = -1;
    pushSet(out,netfd,set_pointer);
    return 0;
} 
